const {
  Http
} = require("../../utils/http")
Page({
  onLoad(options) {
    this.setData({
      store_id: options.store_id,
      phone: options.phone,
      username: options.username,
      mack: options.mack,
      money: options.money,
    })
    console.log('Page Load...');
  },
  onShow: function () {
    this.passwordBox = this.selectComponent('#passwordBox');
  },
  data: {
    tipText: '请输入六位数字密码',
    // 用于页面样式的
    valueIsShow: false,
    // 记录临时的值，点击按钮后再保存到对应变量中
    currentValue: '',
    firstValue: '',
    secondValue: '',
    store_id: '',
    phone: '',
    username: '',
    mack: '',
    money: ''
  },
  navigatorTo(e) {
    let url = e.currentTarget.dataset.url;
    wx.navigateTo({
      url: url
    })
  },
  // 调用组件中的方法
  toggleValue() { //显示隐藏密码

    this.setData({
      valueIsShow: !this.data.valueIsShow
    })
    this.passwordBox.toggleValue()
  },

  inputChange(e) {
    let currentValue = e.detail
    this.setData({
      currentValue
    })
  },

  saveInputValue() { //第二次 输入密码
    let value = this.data.currentValue
    if (value.length < 6) {
      return
    }
    if (this.data.firstValue == '') {
      // 调用组件中的方法，清除之前的值
      this.passwordBox.clearCurrentValue();
      this.passwordBox.toggleValue(false);
      // 重置页面初始的数据，以及文案的修改
      console.log(value);
      this.setData({
        firstValue: value,
        currentValue: '',
        valueIsShow: false,
        tipText: '请再次确认6位数数字密码'
      })
    } else {
      this.setData({
        secondValue: value
      })
    }
  },
  checkPassword() { //判断两次密码是否相同
    this.saveInputValue();

    if (this.data.firstValue != this.data.currentValue) {
      wx.showToast({
        title: "两次密码不同,请重试",
        duration: 2000,
        icon: 'none'
      });
      this.passwordBox.clearCurrentValue();
      this.passwordBox.toggleValue(false);
      this.setData({
        tipText: '请输入六位数字密码',
        firstValue: '',
        currentValue: ''
      })
    } else {
      this.onClickButton();
      console.log('验证密码...' + this.data.currentValue);
    }
  },

  onClickButton: async function () {
    let that = this;
    console.log('ok');
    let result = await Http.request({
      url: 'wechat/user/set_payment_password',
      data: {
        token: wx.getStorageSync('token'),
        payment_password: this.data.currentValue
      },
      method: "POST"
    });
    console.log(result);
    if (result.code == 0) {
      wx.showToast({
        title: result.msg,
        icon: 'success',
        duration: 2000,
        complete() {
          switch (that.data.mack) {
            case 1:
              wx.redirectTo({
                url: '/pages/pay/index?store_id=' + that.data.store_id + '&phone=' + that.data.phone + '&username=' + that.data.username + '&show=' + true,
              });
            case 2:
              wx.redirectTo({
                url: '/pages/cardpay/index?store_id=' + that.data.store_id + '&phone=' + that.data.phone + '&username=' + that.data.username + '&show=' + true,
              });
            case 3:
              wx.redirectTo({
                url: '/pages/offlinpay/index?money=' + this.data.money,
              });
          }

        }
      })
      wx.setStorageSync('payment_password', 1);
      wx.navigateBack();
    } else {
      wx.showToast({
        title: result.msg,
        icon: 'success', //当icon：'none'时，没有图标 只有文字
        duration: 2000
      })
    }
  }
})